package com.haixiaoke.saas.order.state.role;

import cn.hutool.core.util.IdcardUtil;
import cn.hutool.json.JSONUtil;
import com.haixiaoke.saas.common.constant.OrderConstants;
import com.haixiaoke.saas.common.enums.OrderStateEnum;
import com.haixiaoke.saas.common.enums.OrderTabEnum;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.common.tenant.TenantContext;
import com.haixiaoke.saas.houseMember.domain.HouseMember;
import com.haixiaoke.saas.order.domain.Order;
import com.haixiaoke.saas.order.domain.OrderAuditRecord;
import com.haixiaoke.saas.order.domain.OrderLessee;
import com.haixiaoke.saas.order.state.server.IOrderStateServerRole;
import com.haixiaoke.saas.system.domain.LoginUser;
import com.haixiaoke.saas.system.framework.security.util.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * 待签约状态服务方实现类
 *
 * @author qixi
 */

public class SignOrderOrderStateServerRole extends AbstractSpecificOrderOrderStateServerRole {
    private static final Logger LOG = LoggerFactory.getLogger(SignOrderOrderStateServerRole.class);

    public SignOrderOrderStateServerRole(IOrderStateServerRole stateServerRole) {
        super(stateServerRole);
    }


    @Override
    public void sign(Order order) {

       /* OrderLessee orderLessee = order.getOrderLessee();
        orderLessee.setOrderId(order.getOrderId());
        orderLessee.setHouseId(order.getHouseId());
        orderLessee.setTenantId(TenantContext.getTenantId());

        orderLessee.setLesseeState(OrderLessee.LESSEE_STATE_AFOOT);

        orderLesseeService.insertOrderLessee(orderLessee);

        HouseMember houseMember = new HouseMember();
        houseMember.setTenantId(order.getTenantId());
        houseMember.setDeptId(order.getDeptId());
        houseMember.setHouseId(order.getHouseId());
        houseMember.setMemberType(HouseMember.MEMBER_TYPE_TENANTRY);
        houseMember.setMemberName(orderLessee.getLesseeName());
        houseMember.setMemberPhone(orderLessee.getPhone());
        houseMember.setMemberState("1");
        houseMember.setIdCard(orderLessee.getCardNumber());
        houseMember.setIdCardImgs(orderLessee.getCardImgs());
        houseMember.setOrderId(order.getOrderId());
        houseMemberService.insertHouseMember(houseMember);*/


        addOrderStateRecord(order.getOrderId(), order.getOrderState(), OrderStateEnum.ORDER_STATE_SIGNED.getValue());

        order.setOrderState(OrderStateEnum.ORDER_STATE_SIGNED.getValue());
        order.setOrderTab(OrderTabEnum.ORDER_TAB_UNDER_LEASE.getValue());
        order.setRealityCheckInTime(new Date());
        orderService.updateOrder(order);
    }

    /**
     * 签约审核
     *
     * @param order
     */
    @Override
    public void signAudit(Order order) {
        addOrderStateRecord(order.getOrderId(), order.getOrderState(), OrderStateEnum.ORDER_STATE_SIGNED_CHECK.getValue());
        order.setOrderState(OrderStateEnum.ORDER_STATE_SIGNED_CHECK.getValue());

        LoginUser loginUser = SecurityUtils.getLoginUser();
        String tenantId = loginUser.getTenantId();
        OrderAuditRecord orderAuditRecord = new OrderAuditRecord();
        orderAuditRecord.setOrderContent(JSONUtil.toJsonStr(order));
        orderAuditRecord.setTenantId(tenantId);
        orderAuditRecord.setOrderId(order.getOrderId());
        orderAuditRecord.setAuditRecordType(OrderConstants.AUDIT_RECORD_TYPE_SIGN);
        orderAuditRecord.setAuditRecordState(OrderConstants.AUDIT_RECORD_STATE_AUDIT);
        orderAuditRecord.setAddress(order.getPremisesAddress());

        // 获取承租者
        OrderLessee orderLessee = order.getOrderLessee();
        if (OrderLessee.LESSEE_TYPE_PERSONAL.equals(orderLessee.getLesseeType())) {
            int genderByIdCard = IdcardUtil.getGenderByIdCard(orderLessee.getCardNumber());
            // genderByIdCard  1男  2女
            orderLessee.setLesseeSex(genderByIdCard == 1 ? OrderLessee.LESSEE_SEX_MAN : OrderLessee.LESSEE_SEX_WOMAN);
        }
        orderAuditRecord.setLodgerName(orderLessee.getLesseeName());
        orderAuditRecord.setLodgerPhone(orderLessee.getPhone());
        String username = loginUser.getUsername();
        orderAuditRecord.setCreateBy(username);
        orderAuditRecordService.insertOrderAuditRecord(orderAuditRecord);


        orderService.updateOrder(order);
    }

    @Override
    public void approved(Order order) {
        this.sign(order);


    }

    @Override
    public void disagree(Order order) {
        String orderId = order.getOrderId();
        // 原始订单
        Order originalOrder = orderService.selectOrderByOrderId(orderId);
        originalOrder.setOrderState(OrderStateEnum.ORDER_STATE_TO_BE_SIGNED.getValue());

        addOrderStateRecord(originalOrder.getOrderId(), order.getOrderState(), OrderStateEnum.ORDER_STATE_TO_BE_SIGNED.getValue());
        orderService.updateOrder(originalOrder);

    }

    @Override
    public void changeOrder(Order order) {
        if (orderService.checkRepeatOrder(order)) {
            throw new ServiceException("房间在入住时间内已出租");
        }
        orderService.updateOrder(order);
    }

    @Override
    public void cancel(Order order) {
        addOrderStateRecord(order.getOrderId(), order.getOrderState(), OrderStateEnum.ORDER_STATE_CANCEL.getValue());
        order.setOrderState(OrderStateEnum.ORDER_STATE_CANCEL.getValue());
        order.setOrderTab(OrderTabEnum.ORDER_TAB_FAILURE.getValue());
        orderService.updateOrder(order);
    }


    /**
     * 超时未签约
     *
     * @param order
     */
    @Override
    public void preOrderSignOvertime(Order order) {
        addOrderStateRecord(order.getOrderId(), order.getOrderState(), OrderStateEnum.ORDER_STATE_OVERTIME.getValue());
        order.setOrderState(OrderStateEnum.ORDER_STATE_OVERTIME.getValue());
        order.setOrderTab(OrderTabEnum.ORDER_TAB_FAILURE.getValue());
        orderService.updateOrder(order);
    }
}
